Skip to main content

Full text of "Programming"

See other formats

Beginning Programming: Learning Python 


Make Projects 

build, hack, tweak, share, discover^ 

Beginning Programming: 
Learning Python 

Written By: Chandler 


Programming is one of the key components in the technology in our lives today. From things 
like video games to even the computer on which you are reading this, programs are needed 
to make them operate. And if you want to learn how to program, this tutorial is where to 

In this tutorial, you will learn about the programming language Python (yes, that is the name 
of a snake, but in this case the language was named after the television show Monty 
Python). Python is a great language to start with because of the ease with which you can 
write programs. Python doesn't require a huge structure of classes and methods to get your 
program running. However, for those of you who are advanced in programming, Python is 
optionally object-oriented. This makes it quick and simple to write programs when simplicity 
is needed, while using the same language you can create advanced programs with multiple 
classes and methods. 

© Make Projects Page 1 of 10 

Beginning Programming: Learning Python 

Step 1 — Beginning Programming: Learning Python 

i* puthon' 


Advanced Search 

* Download Windows 


Python for Microsoft Windows (XP and later releases) 


Python releases include an excellent Windows installer, See the individual releases for details. 



You may also wish to download Win32all. Mark Hammond's add-on that includes the Win32 API, 

COM support, and Fythonwin extensions. It's available from the pywin32 project on 





Python for Windows CE 


The PythonCE project on Sourceforge has binaries ol 2.5 for StrongArm available. 
There is an active PythonCE list. 





Python Wlkl 

Python Insid&r Blc-g 

Python 2 or 3"? 

Help Maintain Web&ite 

Help Fundi Python 



Copyrights 1990-2O11. Pylhcn Software Foundation Website mainEai ned by the Python tommunily 

Legal Statwnente hosting by xs4all 1 design by Tim Parkin 

Non-Priglish Resource's 

• Setup: 

• Welcome to programming! If you 
have seen my tutorial on OOP 
(Object-Oriented Programming), let 
me tell you that this tutorial will be 
much easier to follow. 

• To start, go to the official Python 
programming language website and 
click on the Download link. Click on 
your operating system in the list 
(Windows, Macintosh, or other) 
and follow the instructions on the 
page. When done, open the Python 
Interactive Prompt. To do that, 
search for "IDLE" and open it. IDLE 
is an IDE (Integrated Development 
Environment); in this case it stands 
for Integrated DeveLopment 
Environment, spelled IDLE after 
Eric Idle, one of the actors on 
Monty Python. Once you're at the 
Python interactive prompt, the fun 
can begin. 

• If you can't find IDLE, search for 
"Python" and see if you can find 
something along the lines of 
"Python (Command Line)". 

© Make Projects 

Page 2 of 10 

Beginning Programming: Learning Python 

Step 2 

Python 2.7.1+ (r27l:8G832, Apr 11 20ll r 18:05:24) 
[GCC 4.5.2] on linux2 

Type "copyright", 'credits" or "license [J " far more informati 
=== No Subprocess ---- 
>» print "Hi!" 

• Learning Simple Commands: 

• Once in IDLE or Command Line 
Python, type: 

• print "Hi! " 

• next to the little >» and hit Enter. 
It prints (writes to the screen) "Hi!" 
and another >» appears ! That was 
pretty simple, right? 

• Now you know the print command. 
Just type print and write what 
you want it to print after it in 
quotation marks! (The quotation 
marks are very important - without 
them, Python mistakes Hi ! for a 

Step 3 

• Note: since Python version 3, print can be used without parentheses around what you want 
to print. If you are using a later version, or are experiencing problems trying to use this 

newer version, just use print ( "Text goes here"). 

© Make Projects 

Page 3 of 10 

Beginning Programming: Learning Python 

Step 4 

python 2.7.:- (i-2 : : : £-bB32 r Apr :: ::.., :::: = :2-;i 

[GCC 4 .5.21 on Iinux2 

lype "copyright", "credits" or "licensed" for more information. 

==== No Subprocess ==== 

>» print "Hi ! " 


>» x - 5 

>» prir.r x 



• Let's try something new! Variables 
are just like they were in algebra: 
they give names to numbers whose 
values may or may not be known. 
Python's variables are very similar. 
I'll show an example. 

• x = 5 

• print x 

• Did you see what it did? It stored 
the number 5 into the variable x. 
Then, whenever you say x in a 
program, the computer thinks "5!" 
So print x is equivalent to 
print 5. Make sense so far? 

© Make Projects 

Page 4 of 10 

Beginning Programming: Learning Python 

Step 5 

Python 2.7.1+ (r271:S6832, Apr II 2011, 18:05:24) 

[GCC 4.5.2] on linux2 

Type "copyright", "credits" or "license!)" fox more information. 

==== No Subpiocess ==== 

»> print "Hi! " 


>» x = 5 

>» print x 


»> y = 5 

>» z - S 

>» pcint y + z 



• Python can do math, too. Type in: 

• y = 5 

• z = 6 

• print y + z 

• Did you guess what would happen 
before you typed this in? After you 
typed all of that in, you probably 
saw "11" if you typed it correctly. 
It's just like the last example, but it 
replaces two variables. When it 
sees y, it replaces it with 5. Same 
for z and 6. So when it comes 
across print y + z, it sees 

print 5 + 6! 

• Note: you can also do subtraction 
(-), multiplication, (*), division (/), 
and much more. 

© Make Projects 

Page 5 of 10 

Beginning Programming: Learning Python 

Step 6 

Python 2.7.1+ (r271:S6B32, Apr 11 2011 r 18:05:24) 

[GCC 4.5.2] on linux2 

Type "copyright", "credits" or "license [J ' for more infonuati 

No Subprocess 

>» print "Hi!" 

>» y - 5 

>» z = S 

>» print y + z 


>» y += 4 

»> print y 

• Say you want to add 4 to y. You 
could type this: 

• y = y + 4 

• It does work. It may seem 
confusing, but all it's doing is 
setting y (on the left side) to 4 
more than it already was. Rather 
than doing that, Python has a 
shortcut that makes things much 
less confusing: 

• y += 4 

• After typing the code above, print y 
by typing the following code: 

• print y 

• It will, as you may have guessed, 
print out "9". 

© Make Projects 

Page 6 of 10 

Beginning Programming: Learning Python 

Step 7 

© Make Projects Page 7 of 1 

Beginning Programming: Learning Python 

Python 2.7.1+ [r271 
IGCC 4.5.2] on linu 

:B6832 r 




1 : 



Type "copyright "| r " 


■ or 



for moie 



==== No Subprocess 


>» print n ^i ! " 

>» x = 5 

>» print x. 


>» y = 5 

»> z = 6 

>>> print y + z 


>» y += 4 

>» print y 


»> if y == 9: 

print "Y is 


to 9 

Y is equal to 9! 


• Now that you've got that done, let's 
move on to letting your code make 
its own decisions. We aren't going 
to make an Al program (sadly), but 
we will learn about the if 
statement. Have you ever had a 
thought that began with "What 
if..."? For instance, "What if I forgot 
to close the door?" The answer can 
vary in intensity, but one example 
could be that a lot of money might 
be wasted on your heater. The if 
statement works in a very similar 

• Here's how it works: 

• if statement: 

• [tab] actions 

• Here's an example. (Note: IDLE 
automatically adds the tab, and for 
IDLE and command-line Python, in 
this example, you have to press 
Enter two times after the last 

• if y == 9; 

• [tab] print "Y is equal to 
nine ! " 

• The "==" might be confusing, but 
"=" was already taken for giving 
values to variables, so Python had 
to find a new symbol for if 
statements to indicate equality. 
Think about it this way: "=" is like a 
statement, and "==" is like a 
question (Does y = 9?). There are 

© Make Projects 

Page 8 of 10 

Beginning Programming: Learning Python 

also "<" (less than), ">" (greater 
than), "!=" (not equal to), "<=" (less 
than or equal to), and ">=" (greater 
than or equal to). 

© Make Projects 

Page 9 of 10 

Beginning Programming: Learning Python 

Step 8 

Uj, ^Open . _2;Save J, Undo Q^ CJg 

testpy X [ 

^, = 5 

y = s 

if x = y: 

print "X * T!" 

• Nice job so far. If you've made it 
here, you will likely make it through 
the entire series of tutorials. Before 
I end this tutorial, I'd like to show 
one last trick: executing Python 

• Open a new text document, and 
name it You can also go to 
File -> New Window in IDLE. 
Inside, type in the following code: 

• x = 5 

• y = 5 

• if x == y: 

• [tab] print "X = Y! " 

• Now, go to Terminal, the Command 
Prompt, or whatever alternative 
you have on your system and 
change your directory to where 
your file is. 

• To do that, type cd (for all 
systems) and then the directory 
that your file is in. For instance 
(Linux example), type cd 

/home /user name /Desk top. 

Once done with that, type python 
and then the name of your file (also 
all systems). Example: python 
test . py. 

This document was last generated on 2012-11-03 04:26:35 AM. 

© Make Projects 

Page 10 of 10